Skip to content

fix(player): reset playback speed to 1x at the live head - #4347

Merged
ImprovedTube2 merged 2 commits into
code-charity:masterfrom
8rulerstar:fix/4346-live-edge-speed
Oct 24, 2026
Merged

ImprovedTube2 merged 2 commits into
code-charity:masterfrom
8rulerstar:fix/4346-live-edge-speed

Conversation

@8rulerstar

@8rulerstar 8rulerstar commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Summary

Resolves #4346

When a livestream is sped up through ImprovedTube (e.g. 5x) and catches up to the
live head, playback gets stuck in an endless stall → buffer → play loop.

Cause

ImprovedTube.playbackSpeed() sets video.playbackRate directly, so YouTube's player
still reports 1x (player.getPlaybackRate() returns 1 while the video runs at 5x).
YouTube's own speed menu drops back to 1x as soon as it reaches the live head, but
since it doesn't know about our speed, it never does here.

Measured on a live stream (Chrome):

result
player.setPlaybackRate(2) (YouTube's API) one stall at the live head, then YouTube switches to 1x and plays normally
video.playbackRate = 5 (what ImprovedTube does) ~1s playing / ~4s waiting, repeating forever, rate stays at 5

Changes

  • player.js: add playerLiveHeadSpeedReset. On waiting, if the video is live,
    player.getProgressState().isAtLiveHead is true and the rate is > 1, set the rate
    to 1 and show the usual speed status. This mirrors what YouTube's player already does.
  • functions.js: attach it as a waiting listener next to the existing player listeners.
  • tests/unit/live-head-speed-reset.test.js: covers the reset, DVR playback behind the
    live head (speed kept), non-live videos, rates ≤ 1, and a missing player API.

Speeds above 2x stay available when watching a livestream behind the live head
(YouTube's own menu caps live at 2x); normal videos are not affected.

Testing

  • npm test: 124 passed (8 new). The new tests fail without the fix.
  • Live stream, fix injected: at 5x it stalls once at the live head, switches to 1x
    and plays normally. After seeking 80s back and setting 5x, it keeps 5x while behind
    and switches to 1x only when it catches up.

@wahajahmed010 wahajahmed010 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Solid bug fix with good test coverage. The diagnosis is excellent: measuring the difference between YouTube's API speed and direct video.playbackRate manipulation and tying it to the missing live-head reset is the kind of root-cause work that ages well. Notes:

1. Hook placement is correct. Binding playerLiveHeadSpeedReset to waiting in playerOnPlay (and unbinding/rebinding on each play) matches how the existing playerOnEnded listener is managed right above it. Consistent with the surrounding code style.

2. The four guards are all necessary:

  • video.playbackRate > 1 (only relevant when sped up; no work at 1x)
  • video.closest('.html5-video-player') with fallback to ImprovedTube.elements.player (covers environments where closest doesn't return the player, e.g., older YouTube layouts)
  • getVideoData().isLive (only applies to live streams)
  • getProgressState().isAtLiveHead === true (only when actually at the live head, not while DVR-watching behind it)

The test suite exercises each of these branches. Excellent coverage.

3. isAtLiveHead !== true (strict). You correctly use strict inequality, so undefined (older API) bails out. If isAtLiveHead is missing because the API isn't available, you do nothing. Safe default.

4. showStatus(1) UX. Resetting the speed at the live head is silent to the user beyond a status toast. Some users may find the sudden drop from 5x to 1x jarring; a future enhancement could be to ramp the speed gradually. Not for this PR, just a thought.

5. waiting is the right event. stalled would also work but fires less reliably across browsers. waiting is the HTML5 spec's official "no data right now" event. Good choice.

6. The test harness is well-built. Using vm.runInNewContext with a minimal ImprovedTube shim and setup({...}) factory is much cleaner than monkey-patching globals. The 5 test cases cover the main branches and an isAtLiveHead !== true boundary. The test.each([1, 0.75]) parameterization for non-sped-up rates is a nice touch.

7. One nit on test coverage. You don't test the closest() fallback path (when closest returns null, the code falls back to ImprovedTube.elements.player). It's exercised by the "does nothing when the player API is unavailable" test indirectly, but a dedicated test where closest returns null and elements.player is undefined would prove the early-return path more directly. Optional.

8. No regressions I can spot. The added lines are purely additive. The playerOnPlay body has two extra removeEventListener/addEventListener pairs that mirror the existing ended listener handling, same pattern, same place.

Not blocking. This is a high-quality fix with above-average test depth. LGTM with the optional closest() fallback test.

@8rulerstar

Copy link
Copy Markdown
Member Author

Added a dedicated test for the closest() fallback path — and went one step further than requested: a second case where elements.player is actually set, proving the fallback resets the speed rather than just failing safe.

Ran a mutation sweep across the function's guard clauses afterward (each condition individually broken, then the suite rerun): 7/8 are caught, the miss being priority between closest() and elements.player in the ||, which doesn't affect real behavior since closest() always resolves the player in-browser.

Thanks for the thorough review!

…ed reset

closest('.html5-video-player') can return null; add a dedicated case for
that with no elements.player set (proves the early return) and one with
a valid elements.player (proves the fallback actually works, not just
fails safe).
@8rulerstar
8rulerstar force-pushed the fix/4346-live-edge-speed branch from 6f16205 to e10bb87 Compare October 7, 2026 06:20
@ImprovedTube2
ImprovedTube2 merged commit dc038c7 into code-charity:master Oct 24, 2026
@8rulerstar
8rulerstar deleted the fix/4346-live-edge-speed branch October 30, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞Infinite freezing loop when livestream playback speed is > 1x (Distinct from #4043)

3 participants